package cn.ac.iscas.dao.mapper.dameng;

import cn.ac.iscas.dao.entity.ComponentMetadataDO;
import cn.ac.iscas.dao.provider.dameng.DamengComponentMetadataProvider;
import cn.ac.iscas.dao.provider.mysql.MysqlComponentMetadataProvider;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/7/26 11:27
 */

@Mapper
//使用mysql数据库
@DS("master")
public interface IDamengComponentMetadataMapper {

    @Select({
            "SELECT " +
                    " _id, entryClassName, kind, alias, description, icon ,modelType ,algorithm ,type, " +
                    " modelLocalPath , modelPublishPath,columnConfig, viewParameter " +
                    " FROM componentmetadata "
    })
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="entryClassName", property="entryClassName", jdbcType=JdbcType.VARCHAR),
            @Result(column="kind", property="kind", jdbcType=JdbcType.VARCHAR),
            @Result(column="alias", property="alias", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelType", property="modelType", jdbcType=JdbcType.VARCHAR),
            @Result(column="algorithm", property="algorithm", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelLocalPath", property="modelLocalPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelPublishPath", property="modelPublishPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="columnConfig", property="columnConfig", jdbcType=JdbcType.VARCHAR),
            @Result(column="viewParameter", property="viewParameter", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    List<ComponentMetadataDO> selectAll();

    @SelectProvider(type = DamengComponentMetadataProvider.class, method = "select")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="entryClassName", property="entryClassName", jdbcType=JdbcType.VARCHAR),
            @Result(column="kind", property="kind", jdbcType=JdbcType.VARCHAR),
            @Result(column="alias", property="alias", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelType", property="modelType", jdbcType=JdbcType.VARCHAR),
            @Result(column="algorithm", property="algorithm", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelLocalPath", property="modelLocalPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelPublishPath", property="modelPublishPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="columnConfig", property="columnConfig", jdbcType=JdbcType.VARCHAR),
            @Result(column="viewParameter", property="viewParameter", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    List<ComponentMetadataDO> select(final String type, final String modelType);

    @SelectProvider(type = DamengComponentMetadataProvider.class, method = "selectByCategory")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="entryClassName", property="entryClassName", jdbcType=JdbcType.VARCHAR),
            @Result(column="kind", property="kind", jdbcType=JdbcType.VARCHAR),
            @Result(column="alias", property="alias", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelType", property="modelType", jdbcType=JdbcType.VARCHAR),
            @Result(column="algorithm", property="algorithm", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelLocalPath", property="modelLocalPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelPublishPath", property="modelPublishPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="columnConfig", property="columnConfig", jdbcType=JdbcType.VARCHAR),
            @Result(column="viewParameter", property="viewParameter", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    List<ComponentMetadataDO> selectByCategory(final String category);

    @Insert({
            "INSERT INTO " +
                    " componentmetadata " +
                    " ( _id, entryClassName, kind, alias, description, icon ,modelType ,algorithm ,type, modelLocalPath , modelPublishPath, columnConfig ) ",
            " VALUES (#{_id,jdbcType=VARCHAR}, #{entryClassName,jdbcType=VARCHAR}, #{kind,jdbcType=VARCHAR}, ",
            " #{alias,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR}, #{modelType,jdbcType=VARCHAR}, ",
            " #{algorithm,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{modelLocalPath,jdbcType=VARCHAR}, ",
            " #{modelPublishPath,jdbcType=VARCHAR}, #{columnConfig,jdbcType=VARCHAR})"
    })
    int insert(ComponentMetadataDO componentMetadata);

    @SelectProvider(type = DamengComponentMetadataProvider.class, method = "selectById")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="entryClassName", property="entryClassName", jdbcType=JdbcType.VARCHAR),
            @Result(column="kind", property="kind", jdbcType=JdbcType.VARCHAR),
            @Result(column="alias", property="alias", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelType", property="modelType", jdbcType=JdbcType.VARCHAR),
            @Result(column="algorithm", property="algorithm", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelLocalPath", property="modelLocalPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="modelPublishPath", property="modelPublishPath", jdbcType=JdbcType.VARCHAR),
            @Result(column="columnConfig", property="columnConfig", jdbcType=JdbcType.VARCHAR),
            @Result(column="viewParameter", property="viewParameter", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    ComponentMetadataDO selectById(final String id);

    @Delete("<script>" +
            "DELETE FROM componentmetadata WHERE _id IN " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    @Results(value = { @Result(column = "_id", property = "_id") })
    int deleteBatch(@Param("ids") List<String> ids);
}
